﻿Public Class RHGROUP
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_MASRHGROUP As New PROVIDERSCLASS

    Private Sub RHGROUP_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridRhgroup()

        DesignForm(Me)
        DesignGrid(masrhgroupGrid)

        PROVIDER_MASRHGROUP.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridRhgroup()
        Dim objSelectDataMASRHGROUP As New masrhgroup
        Dim MASRHGROUP As DataTable
        MASRHGROUP = connect.GetTable(objSelectDataMASRHGROUP.selectMasrhgroup_)
        masrhgroupGrid.PrimaryGrid.DataSource = MASRHGROUP
    End Sub

    Private Sub masrhgroupGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles masrhgroupGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสเลือด RH").Value() IsNot DBNull.Value Then
            rhgroupIDTextbox.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสเลือด RH").Value()
        Else
            rhgroupIDTextbox.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("กลุ่มเลือด RH").Value() IsNot DBNull.Value Then
            rhgroupnameTextbox.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("กลุ่มเลือด RH").Value()
        Else
            rhgroupnameTextbox.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            rhgroupstatusCheckbox.Checked = True
        Else
            rhgroupstatusCheckbox.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE masrhgroup SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `rhgroup` = '" & rhgroupIDTextbox.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connectlocal.BeginTrans()
        '        Sql = "UPDATE MASRHGROUP SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [RHGROUP] = '" & rhgroupIDTextbox.Text & "' ;"

        '        connectlocal.ExecuteNonQuery(Sql)
        '        connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        addMasrhgroup.Enabled = False
    End Sub

    Private Sub addMasrhgroup_Click_1(sender As Object, e As EventArgs) Handles addMasrhgroup.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASRHGROUP.PADD_ = True Then
                Dim objInsertData As New masrhgroup
                objInsertData.rhgroupDesc_ = rhgroupnameTextbox.Text
                objInsertData.rhgroupStatus_ = rhgroupstatusCheckbox.Checked
                objInsertData.insertMasrhgroupData()

                SelectDatagridRhgroup()

                rhgroupIDTextbox.Text = ""
                rhgroupnameTextbox.Text = ""
                rhgroupstatusCheckbox.Checked = False
            Else
                MsgBox(PROVIDER_MASRHGROUP.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMasrhgroup_Click_1(sender As Object, e As EventArgs) Handles editMasrhgroup.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASRHGROUP.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลตำแหน่งดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New masrhgroup
                    objEditData.rhgroupID_ = CInt(rhgroupIDTextbox.Text)
                    objEditData.rhgroupDesc_ = rhgroupnameTextbox.Text
                    objEditData.rhgroupStatus_ = rhgroupstatusCheckbox.Checked
                    objEditData.editMasrhgroupData()

                    SelectDatagridRhgroup()

                    rhgroupIDTextbox.Text = ""
                    rhgroupnameTextbox.Text = ""
                    rhgroupstatusCheckbox.Checked = False
                End If
                addMasrhgroup.Enabled = True
            Else
                MsgBox(PROVIDER_MASRHGROUP.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(masrhgroup.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(masrhgroup.Text)
        nextform.ShowDialog()
    End Sub
End Class